package com.dataStructure.random;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Feed 流
 * 都扩散，
 * 写扩散不方便
 */
public class Twitter {
    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(2);
        queue.offer(4);
        queue.offer(2);
        queue.offer(3);

       queue.poll();

        ArrayList<Integer> list = new ArrayList<>(queue);
        System.out.println(list);
    }
    //自增ID
    public static   Integer twitterId = 0;


    //获取下一位id
    public static Integer getAndIncr() {
        return twitterId++;
    }
    //用户推文前10条，优先队列，id自增就是时间
    private Map<Integer, ArrayList<Post>> userPost = new HashMap<>();

    //用户关注关系
    private Map<Integer, Set<Integer>> userFollow = new HashMap<>();


    public Twitter() {

    }


    public void postTweet(int userId, int tweetId) {
        ArrayList<Post> postList = userPost.get(userId);
        if (Objects.isNull( postList)) {
            postList = new ArrayList<>();
            userPost.put(userId, postList);
        }
        Post post = new Post(getAndIncr(), tweetId);
        postList.add(post);
    }

    //
    public List<Integer> getNewsFeed(int userId) {
        PriorityQueue<Post> queue = new PriorityQueue<>(10, (o1, o2) -> o2.getTime() - o1.getTime());
        //只返回自己的
        ArrayList<Post> queue1 = userPost.get(userId);
        if (queue1 != null) {
            for (Post post : queue1) {
                queue.offer(post);
                if (queue.size() > 10) {
                    queue.poll();
                }
            }
        }
      Set<Integer> followeeIds = userFollow.get(userId);
        //关注其他人
        if (followeeIds != null) {
            //还要聚合 关注的人
            for (Integer followeeId : followeeIds) {
                ArrayList<Post> postQueue = userPost.get(followeeId);
                if (postQueue != null) {
                    for (Post post : postQueue) {
                        queue.offer(post);
                        if (queue.size() > 10) {
                            queue.poll();
                        }
                    }
                }
            }
        }
        System.out.println(queue);
        List<Post> result = queue.stream().sorted((o1, o2) -> o2.getTime() - o1.getTime()).collect(Collectors.toList());
        ArrayList<Integer> list = new ArrayList<>(result.size());
        for (Post post : result) {
            list.add(post.getTwitterId());
        }
        return list;
    }

    public void follow(int followerId, int followeeId) {
        Set<Integer> followeeIdList = userFollow.get(followerId);
        if (Objects.isNull( followeeIdList)) {
            followeeIdList = new HashSet<>();
            userFollow.put(followerId, followeeIdList);
        }
        followeeIdList.add(followeeId);
    }

    public void unfollow(int followerId, int followeeId) {
        Set<Integer> followeeIdList = userFollow.get(followerId);
        if (Objects.isNull(followeeIdList)) {
        } else {
            followeeIdList.removeIf(integer -> integer == followeeId);
        }

    }
}
class Post {
    private Integer time;
    private Integer twitterId;

    public Post(Integer time, Integer twitterId) {
        this.time = time;
        this.twitterId = twitterId;
    }

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    public Integer getTwitterId() {
        return twitterId;
    }

    public void setTwitterId(Integer twitterId) {
        this.twitterId = twitterId;
    }

    @Override
    public String toString() {
        return "Post{" +
                "time=" + time +
                ", twitterId=" + twitterId +
                '}';
    }
}
